﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace yf倒计时
{
    public partial class Form1 : Form
    {


        TimeSpan dtTo; //设置开始时间
        bool fullscreen = false;
        Rectangle rect = new Rectangle();
        Form2 f2 = new Form2();
        Form3 fm3 = new Form3();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;//设置每次间隔1s
            timer1.Enabled = false;
            this.Text = "版本"+this.ProductVersion+"运营管理部制作,供集团员工使用";
            label2.Text = "版本"+this.ProductVersion+"运营管理部制作供,集团员工使用";

            f2.Owner = this;
            fm3.Owner = this;
            fm3.ShowDialog();


        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            dtTo = dtTo.Subtract(new TimeSpan(0, 0, 1));
            label1.Text = " "+dtTo.Hours.ToString() + ":" + dtTo.Minutes.ToString() + ":" + dtTo.Seconds;
            //计时开始隐藏按钮
            button2.Visible = button3.Visible = button4.Visible = button5.Visible = button6.Visible =button7.Visible= false;
            button9.Visible = button10.Visible = button11.Visible = button12.Visible = false;

            if (dtTo.TotalSeconds <= 0.0)//当倒计时完毕
            {
                timer1.Enabled = false;   //其中可自行添加相应的提示框或者方法函数
                button1.Text = "开始";
                // MessageBox.Show("时间到！");//ppt全屏时无效
                //this.ShowDialog();
                //////////            Form frm1 = new Form();
                //////////frm1.Text = "我是模态窗口!";
                //////////frm1.ShowDialog();//打开模态窗口（辍对话框）
                //////////MessageBox.Show(frm1.DialogResult.ToString());

                // runbat2(" '时间到！''pause'");
                //@echo off&cmdow @ /top

                //runbat2(@"mshta vbscript: msgbox('时间到', 64,'倒计时' )(window.close)");
                // runbat2(@"echo msgbox '我是提示内容',64,'我是提示标题'>alert.vbs && start alert.vbs && ping -n 2 127.1>nul && del alert.vbs");
                // runbat2(@"echo  '我是提示内容',5000,'我是提示标题'>alert.vbs && start alert.vbs && ping -n 2 127.1>nul && del alert.vbs");能在全屏下弹出 但是界面太丑陋。


                ////////////fullscreen = !fullscreen;//赋相反值 ，循环。点一次全屏，再点还原。   
                ////////////SetFullScreen(fullscreen, ref rect);
                ////////////if (fullscreen)
                ////////////{
                ////////////    this.FormBorderStyle = FormBorderStyle.None;
                ////////////    this.WindowState = FormWindowState.Maximized;//全屏   
                ////////////}
                ////////////else
                ////////////{
                ////////////    this.FormBorderStyle = FormBorderStyle.Sizable;

                ////////////    this.WindowState = FormWindowState.Normal;//还原   
                ////////////}
                //Process.Start("taskkill /f /im explorer.exe");//没有用。
                 f2.ShowDialog();//无论是否模态 无法全屏弹出。
                //runbat2(Properties.Resources.reboot_explorer);////可以任务栏消失的问题。 太丑

                //计时结束 按钮恢复
            button2.Visible = button3.Visible = button4.Visible = button5.Visible = button6.Visible =button7.Visible= true;
                button9.Visible = button10.Visible = button11.Visible = button12.Visible = true;

            }




        }

        public static bool SetFullScreen(bool fullscreen, ref Rectangle rectOld)
        {
            int Hwnd = 0;
            Hwnd = FindWindow("Shell_TrayWnd", null);
            if (Hwnd == 0) return false;
            if (fullscreen)
            {
                ShowWindow(Hwnd, SW_HIDE);
                Rectangle rectFull = Screen.PrimaryScreen.Bounds;
                SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get   
                SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//set   
            }
            else
            {
                ShowWindow(Hwnd, SW_SHOW);
                SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);
            }
            return true;
        }

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern int ShowWindow(int hwnd, int nCmdShow);
        public const int SW_SHOW = 5; public const int SW_HIDE = 0;

        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        private static extern int SystemParametersInfo(int uAction, int uParam, ref Rectangle lpvParam, int fuWinIni);
        public const int SPIF_UPDATEINIFILE = 0x1;
        public const int SPI_SETWORKAREA = 47;
        public const int SPI_GETWORKAREA = 48;

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);






        public static void yinchangfile(string  fileName)
        {

            FileInfo info = new FileInfo(fileName);
            if (info.Exists)
            {
                info.Attributes = FileAttributes.Hidden;
                Console.WriteLine("文件属性改为隐藏");
            }
        }//隐藏指定文件。


        public void runbat2(string strBat)
    {

        //////  var strBat = "你上面的代码";
        /////var strBat = Properties.Resources.onrkeyoldOA.ToCharArray();
        //  File.WriteAllText("1111.bat", Properties.Resources.onrkeyoldOA);//实测生成可用，但中文乱码

        string StrTimeName = "临时文件可以删除" + System.DateTime.Now.ToFileTime() + "pangdahai.bat";
        File.WriteAllText(StrTimeName, @strBat, Encoding.Default);//实测win7不乱码
        yinchangfile(StrTimeName);//美观 ，隐藏文件
        Process.Start(StrTimeName);

    }

    private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (timer1.Enabled == false)
            {
                if (dtTo.TotalSeconds <= 0.0) {
                    MessageBox.Show("请选个时间");
                }
                else
                {
                    timer1.Enabled = true;
                    button1.Text = "暂停";
                    this.TopMost = true;
                    //this.WindowState = FormWindowState.Minimized;//1.11.0.0 计时开始后最小化
                }
            }
            else {
                this.TopMost = false;
                button2.Visible = button3.Visible = button4.Visible = button5.Visible = button6.Visible = button7.Visible = true;
                button9.Visible = button10.Visible = button11.Visible = button12.Visible =  true;
                timer1.Enabled = false;
                button1.Text = "开始";
                //暂停 显示按钮以便重新选。


            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 10, 0);
            label1.Text = "00:10:00";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 15, 0);
            label1.Text = "00:15:00";

        }

        private void button4_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 20, 0);
            label1.Text = "00:20:00";

        }

        private void button5_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 25, 0);
            label1.Text = "00:25:00";

        }

        private void button6_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 30, 0);
            label1.Text = "00:30:00";

        }

        private void button7_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 0, 15);
            label1.Text = "00:00:15";

        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 5, 0);
            label1.Text = "00:05:00";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 3, 0);
            label1.Text = "00:03:00";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 1, 0);
            label1.Text = "00:01:00";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            dtTo = new TimeSpan(0, 7, 0);
            label1.Text = "00:07:00";
        }

        private void button13_Click(object sender, EventArgs e)
        {
           
            fm3.ShowDialog();

        }
    }
}
